<template>
  <el-dialog
    :title="!groupobForm.grouponId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible1">
    <el-form :model="groupobForm" :rules="dataRule" ref="groupobForm" @keyup.enter.native="groupobFormSubmit()" label-width="80px">
    <el-form-item label="楼盘名称" prop="flatsName">
      <el-input v-model="groupobForm.flatsName" placeholder="楼盘名称"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="groupobForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="groupobForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="团购人数" prop="peopleCount">
      <el-input v-model="groupobForm.peopleCount" placeholder="团购人数"></el-input>
    </el-form-item>
    <el-form-item label="开团人" prop="openGroupPeople">
      <el-input v-model="groupobForm.openGroupPeople" placeholder="开团人"></el-input>
    </el-form-item>
    <el-form-item label="开团人id" prop="openGroupPeopleId">
      <el-input v-model="groupobForm.openGroupPeopleId" placeholder="开团人id"></el-input>
    </el-form-item>
    <el-form-item label="楼盘id" prop="flatsId">
      <el-input v-model="groupobForm.flatsId" placeholder="楼盘id"></el-input>
    </el-form-item>
    <el-form-item label="最低开团人数" prop="minPeopleCount">
      <el-input v-model="groupobForm.minPeopleCount" placeholder="最低开团人数"></el-input>
    </el-form-item>
    <el-form-item label="后悔期限 单位：天" prop="regretTime">
      <el-input v-model="groupobForm.regretTime" placeholder="后悔期限 单位：天"></el-input>
    </el-form-item>
    <el-form-item label="后悔支付违约金  如：200元" prop="regretPay">
      <el-input v-model="groupobForm.regretPay" placeholder="后悔支付违约金  如：200元"></el-input>
    </el-form-item>
    <el-form-item label="团购抽取比例  购房省下的钱*比例" prop="extractionRate">
      <el-input v-model="groupobForm.extractionRate" placeholder="团购抽取比例  购房省下的钱*比例"></el-input>
    </el-form-item>
    <el-form-item label="团购的其他优惠" prop="grouponDesc">
      <el-input v-model="groupobForm.grouponDesc" placeholder="团购的其他优惠"></el-input>
    </el-form-item>
    <el-form-item label="最大团购人数" prop="maxPeople">
      <el-input v-model="groupobForm.maxPeople" placeholder="最大团购人数"></el-input>
    </el-form-item>
    <el-form-item label="团购状态  0-未开团  1-团购中  2-成团  3-失效" prop="groupStatus">
      <el-input v-model="groupobForm.groupStatus" placeholder="团购状态  0-未开团  1-团购中  2-成团  3-失效"></el-input>
    </el-form-item>
    <el-form-item label="交付时间" prop="deliveryTime">
      <el-input v-model="groupobForm.deliveryTime" placeholder="交付时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible1 = false">取消</el-button>
      <el-button type="primary" @click="groupobFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible1: false,
        groupobForm: {
          grouponId: 0,
          flatsName: '',
          startTime: '',
          endTime: '',
          peopleCount: '',
          openGroupPeople: '',
          openGroupPeopleId: '',
          flatsId: '',
          minPeopleCount: '',
          regretTime: '',
          regretPay: '',
          extractionRate: '',
          grouponDesc: '',
          maxPeople: '',
          groupStatus: '',
          deliveryTime: ''
        },
        dataRule: {
          flatsName: [
            { required: true, message: '楼盘名称不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          peopleCount: [
            { required: true, message: '团购人数不能为空', trigger: 'blur' }
          ],
          openGroupPeople: [
            { required: true, message: '开团人不能为空', trigger: 'blur' }
          ],
          openGroupPeopleId: [
            { required: true, message: '开团人id不能为空', trigger: 'blur' }
          ],
          flatsId: [
            { required: true, message: '楼盘id不能为空', trigger: 'blur' }
          ],
          minPeopleCount: [
            { required: true, message: '最低开团人数不能为空', trigger: 'blur' }
          ],
          regretTime: [
            { required: true, message: '后悔期限 单位：天不能为空', trigger: 'blur' }
          ],
          regretPay: [
            { required: true, message: '后悔支付违约金  如：200元不能为空', trigger: 'blur' }
          ],
          extractionRate: [
            { required: true, message: '团购抽取比例  购房省下的钱*比例不能为空', trigger: 'blur' }
          ],
          grouponDesc: [
            { required: true, message: '团购的其他优惠不能为空', trigger: 'blur' }
          ],
          maxPeople: [
            { required: true, message: '最大团购人数不能为空', trigger: 'blur' }
          ],
          groupStatus: [
            { required: true, message: '团购状态  0-未开团  1-团购中  2-成团  3-失效不能为空', trigger: 'blur' }
          ],
          deliveryTime: [
            { required: true, message: '交付时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.groupobForm.grouponId = id || 0
        this.visible1 = true
        this.$nextTick(() => {
          this.$refs['groupobForm'].resetFields()
          if (this.groupobForm.grouponId) {
            this.$http({
              url: this.$http.adornUrl(`/groupon/groupon/info/${this.groupobForm.grouponId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.groupobForm.flatsName = data.groupon.flatsName
                this.groupobForm.startTime = data.groupon.startTime
                this.groupobForm.endTime = data.groupon.endTime
                this.groupobForm.peopleCount = data.groupon.peopleCount
                this.groupobForm.openGroupPeople = data.groupon.openGroupPeople
                this.groupobForm.openGroupPeopleId = data.groupon.openGroupPeopleId
                this.groupobForm.flatsId = data.groupon.flatsId
                this.groupobForm.minPeopleCount = data.groupon.minPeopleCount
                this.groupobForm.regretTime = data.groupon.regretTime
                this.groupobForm.regretPay = data.groupon.regretPay
                this.groupobForm.extractionRate = data.groupon.extractionRate
                this.groupobForm.grouponDesc = data.groupon.grouponDesc
                this.groupobForm.maxPeople = data.groupon.maxPeople
                this.groupobForm.groupStatus = data.groupon.groupStatus
                this.groupobForm.deliveryTime = data.groupon.deliveryTime
              }
            })
          }
        })
      },
      // 表单提交
      groupobFormSubmit () {
        this.$refs['groupobForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/groupon/groupon/${!this.groupobForm.grouponId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'grouponId': this.groupobForm.grouponId || undefined,
                'flatsName': this.groupobForm.flatsName,
                'startTime': this.groupobForm.startTime,
                'endTime': this.groupobForm.endTime,
                'peopleCount': this.groupobForm.peopleCount,
                'openGroupPeople': this.groupobForm.openGroupPeople,
                'openGroupPeopleId': this.groupobForm.openGroupPeopleId,
                'flatsId': this.groupobForm.flatsId,
                'minPeopleCount': this.groupobForm.minPeopleCount,
                'regretTime': this.groupobForm.regretTime,
                'regretPay': this.groupobForm.regretPay,
                'extractionRate': this.groupobForm.extractionRate,
                'grouponDesc': this.groupobForm.grouponDesc,
                'maxPeople': this.groupobForm.maxPeople,
                'groupStatus': this.groupobForm.groupStatus,
                'deliveryTime': this.groupobForm.deliveryTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible1 = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
